package com.yoomiito.app.ui.my.psw;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.yoomiito.app.Constant;
import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.utils.UserInfoConfig;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SetPayPswActivity extends BaseActivity<SetPayPswActPresenter> {

    @BindView(R.id.tv_center)
    TextView titleTv;
    @BindView(R.id.act_title_fm)
    FrameLayout mFrameLayout;
    private FragmentManager fragmentManager;

    List<Fragment> mFragments = new ArrayList<>();
    private String title;
    @Override
    public int getLayoutId() {
        return R.layout.act_title_framelayout;
    }

    @Override
    public SetPayPswActPresenter newP() {
        return new SetPayPswActPresenter(App.mAppConfig);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
//        AndroidBug5497Workaround.assistActivity(this);
//        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        titleTv.setVisibility(View.VISIBLE);
        title = UserInfoConfig.havePayPsw()?"修改支付密码":"设置支付密码";

        titleTv.setText(title);
        initFm();
    }

    public static void start(Context context){
        Intent intent = new Intent(context,SetPayPswActivity.class);
        context.startActivity(intent);
    }

    private void initFm() {
        fragmentManager = getSupportFragmentManager();
        mFragments.add(new VerifyCodeFragment());
        mFragments.add(new SetPayPswFragment());
        ChangeResultFragment changeResultFragment = new ChangeResultFragment();
        Bundle bundle = new Bundle();
        bundle.putString(Constant.MSG,UserInfoConfig.havePayPsw()?"支付密码修改成功":"支付密码设置成功");
        changeResultFragment.setArguments(bundle);
        mFragments.add(changeResultFragment);
        switchFm(0);

    }

    public void switchFm(int position){
        if (position==2){
            showLoading();
            getP().getUserInfo();
        }
        if (position==1){
            titleTv.setText("输入支付密码");
        }else {
            titleTv.setText(title);
        }
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.act_title_fm,mFragments.get(position));
        transaction.commitAllowingStateLoss();
    }


    @OnClick({R.id.iv_back_left})
    public void onClick(View view){
        if (R.id.iv_back_left == view.getId()){
            this.finish();
        }
    }

}
